import COS from 'cos-nodejs-sdk-v5'
import { getCloudAlbumSts } from './sts'
import { Router } from 'express'
import _ from 'lodash'
import dayjs from 'dayjs'

const cos = new COS({
  SecretId: process.env.SECRET_ID,
  SecretKey: process.env.SECRET_KEY,
})

export async function getScreenShot () {
  const { Credentials } = await getCloudAlbumSts()
  const cos = new COS({
    SecretId: Credentials.TmpSecretId,
    SecretKey: Credentials.TmpSecretKey,
    SecurityToken: Credentials.Token,
  })
  const res = await cos.getBucket({
    Bucket: 'static-1302789012',
    Region: 'ap-shanghai',
    Prefix: 'personal/pictures/screenshot',
  })
  const arr = res.Contents.map(item => item.Key)
  // 第一个是当前目录不是文件
  arr.shift()
  arr.sort((a, b) => a > b ? -1 : 1)
  for (let i = 0; i < arr.length; i++) {
    arr[i] = cos.getObjectUrl({
      Bucket: 'static-1302789012',
      Region: 'ap-shanghai',
      Key: arr[i],
      Expires: 600, // 单位秒
    }, () => {
    })
  }
  return arr
}

let cachedObjects = []
let cachedUnix = NaN

export async function listObjects () {
  const { Credentials } = await getCloudAlbumSts()
  const cos = new COS({
    SecretId: Credentials.TmpSecretId,
    SecretKey: Credentials.TmpSecretKey,
    SecurityToken: Credentials.Token,
  })
  let NextMarker = ''
  const res = await cos.getBucket({
    Bucket: 'static-1302789012',
    Region: 'ap-shanghai',
    Prefix: 'personal/pictures/camera',
  })
  cachedObjects = res.Contents
  NextMarker = res.NextMarker
  while (NextMarker) {
    const res = await cos.getBucket({
      Bucket: 'static-1302789012',
      Region: 'ap-shanghai',
      Prefix: 'personal/pictures/camera',
      Marker: NextMarker
    })
    cachedObjects.push(...res.Contents)
    NextMarker = res.NextMarker
  }
  cachedUnix = Date.now()
}

export async function getPhoto (pageNum = 1, pageSize = 100) {
  if (!cachedObjects.length) {
    await listObjects()
  }
  const arr = cachedObjects.map(item => item.Key).filter(item => !item.includes('mp4'))
  // 第一个是当前目录不是文件
  arr.shift()
  arr.reverse()
  const data = _.chunk(arr, pageSize)[pageNum - 1] || []
  for (let i = 0; i < data.length; i++) {
    data[i] = cos.getObjectUrl({
      Bucket: 'static-1302789012',
      Region: 'ap-shanghai',
      Key: data[i],
      Domain: 's1.hct.plus'
    }, () => {
    })
  }
  return {
    pageNum,
    pageSize,
    total: arr.length,
    data,
    cachedUnix
  }
}

export async function getEmoji (temp = false) {
  const res = await cos.getBucket({
    Bucket: 'static-1302789012',
    Region: 'ap-shanghai',
    Prefix: temp ? 'temp' : 'emoji'
  })
  const data = res.Contents
  // 第一个是当前目录不是文件
  data.shift();
  data.sort((a, b) => {
    return dayjs(b.LastModified).unix() - dayjs(a.LastModified).unix()
  });
  return data.map(item => item.Key)
}

const router = Router()
// router.use('/getScreenShot', (req, res) => {
//   getScreenShot().then(data => res.send(data))
// })
// router.use('/getPhoto', (req, res) => {
//   getPhoto(req.query.pageNum && +req.query.pageNum, req.query.pageSize && +req.query.pageSize).then(data => res.send(data))
// })
router.use('/getEmoji', (req, res) => {
  getEmoji().then(data => res.send(data))
})
router.use('/getTemp', (req, res) => {
  getEmoji(true).then(data => res.send(data))
})
export default router
